package za.org.jcicada.syshealth;

/**
 * Abstract base class for view component of the system health display MVC
 * implementation. When constructed it attaches to the specified model for
 * updates. After construction it needs to be initialized in order to complete
 * creation of the controller using an abstract factory method (makeController).
 * 
 * @author Robert Crida
 */
public abstract class ViewBase implements View {

	/** MVC Model handle */
	private Model model;

	/** MVC Controller handle */
	private Controller controller;

	/**
	 * Constructor attaches view to model but does not create controller.
	 * initialize() must be called next to complete setup.
	 * 
	 * @param model
	 *            is the model that the view displays.
	 */
	public ViewBase(Model model) {
		this.model = model;
		controller = null;
		model.addObserver(this);
	}

	/**
	 * Initialize the controller to complete setup
	 */
	public void initialize() {
		controller = makeController();
	}

	public Model getModel() {
		return model;
	}

	public Controller getController() {
		return controller;
	}

	/**
	 * Create a controller suitable for this view.
	 * 
	 * @return the controller object to use with the view
	 */
	public abstract Controller makeController();

	/**
	 * Detach from the subject before GC
	 */
	@Override
	protected void finalize() throws Throwable {
		model.deleteObserver(this);
	}

}
